﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.BLL.Mtbl;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.App;
namespace HKEA.CMS.Core.Models
{
    public class M_BrandListDetail
    {
        public bool IsFromApp { get; set; }
        public int URLUID
        {
            get
            {
                if (IsFromApp)
                    return AppBrandUID;
                else
                    return BrandUID;
            }
        }

        public string BrandName_Eng { get; set; }
        public string BrandName_Chi { get; set; }

        public int AppBrandUID { get; set; }
        public int BrandUID { get; set; }
        public int BrandLogo { get; set; }
        public int AppBrandLogo { get; set; }
        public string BrandName
        {
            get
            {
                string str = BrandName_Eng + "&nbsp;" + BrandName_Chi;
                if (BrandLogo > 0 || AppBrandLogo > 0)
                    str += "<img src=\"/Media/Css/images/cphsmedia.png\" />";
                return str;
            }
        }
        public string strSpace{ get{return " / ";}}
        public int Deleted { get; set; }
        public string SourceType { get; set; }
        public string strCat
        {
            get
            {
                string str = string.Empty;
                StringBuilder strBuilder = new StringBuilder();
                if (IsFromApp)
                {

                    app_tblBrand_ProdCatBLL appBLL = new app_tblBrand_ProdCatBLL();
                    app_tblBrand_ProdCat appENT = new app_tblBrand_ProdCat();
                    appENT.AppBrandUID = AppBrandUID;
                    appENT.Deleted = 0;
                    var AllApplist = appBLL.GetListByENT(appENT, false);

                    var app_list = AllApplist.Where(a => a.Deleted == 0).ToList();
                    //ProductCatUID、 并非关联主键！！！等效于SubCat表中的KEY
                    var tbl_withOutList = AllApplist.Where(a => a.BrandUID > 0).Where(a => a.BrandProdCatUID > 0).ToList().Select(a => (int)a.ProductCatUID).ToList();
                    foreach (var temp in app_list)
                    {
                        string strEng = temp.mtblProductSubCat.ProductSubCat_Eng;
                        string strChi = temp.mtblProductSubCat.ProductSubCat_Chi;
                        if (!string.IsNullOrEmpty(strEng))
                            strBuilder.Append(strEng);
                        if (!string.IsNullOrEmpty(strChi))
                            strBuilder.Append("&nbsp;").Append(strChi);
                        strBuilder.Append(strSpace);
                    }
                    if (BrandUID > 0)
                    {
                        tblBrand_ProdCatBLL bll = new tblBrand_ProdCatBLL();
                        List<mtblProductSubCat> subCatList = new List<mtblProductSubCat>();
                        subCatList = bll.GetSubCatListByBrandUID(BrandUID,tbl_withOutList);
                        for (int i = 0; i < subCatList.Count; i++)
                        {
                            var temp = subCatList[i];
                            strBuilder.Append(temp.ProductSubCat_Eng);
                            if (!string.IsNullOrEmpty(temp.ProductSubCat_Chi))
                            {
                                strBuilder.Append("&nbsp;");
                                strBuilder.Append(temp.ProductSubCat_Chi);
                            }

                            if (i < subCatList.Count - 1)
                            { strBuilder.Append(" / "); }
                        }
                        return strBuilder.ToString();
                    }
                }
                else
                {
                    tblBrand_ProdCatBLL bll = new tblBrand_ProdCatBLL();
                    List<mtblProductSubCat> subCatList = new List<mtblProductSubCat>();
                    subCatList = bll.GetSubCatListByBrandUID(BrandUID);
                    for (int i = 0; i < subCatList.Count; i++)
                    {
                        var temp = subCatList[i];
                        strBuilder.Append(temp.ProductSubCat_Eng);
                        if (!string.IsNullOrEmpty(temp.ProductSubCat_Chi))
                        {
                            strBuilder.Append("&nbsp;");
                            strBuilder.Append(temp.ProductSubCat_Chi);
                        }

                        if (i < subCatList.Count - 1)
                        { strBuilder.Append(" / "); }
                    }
                    return strBuilder.ToString();
                }
                str = strBuilder.ToString();
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - strSpace.Length);
                return str;
            }
        }
    }
}
